/*
Copyright (C) 2010 Andrew Dieffenbach (www.puzzud.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

package com.puzzud.engine.input;

import com.puzzud.engine.input.InputObject;

import java.util.Vector;

// InputDevice
public class InputDevice extends InputObject
{
	protected Vector<InputButton> buttonBuffer;
	protected int buttonBufferSizeLimit;
	
	// TODO Make this class protected?
	class InputButton
	{
		public static final int BUTTON_STATE_UP				= -1;
		public static final int BUTTON_STATE_RELEASED			= 0;
		public static final int BUTTON_STATE_PRESSED			= 1;
		public static final int BUTTON_STATE_DOWN			= 2;
		
		public int id;
		public int state;
		
		public InputButton( int id, int state )
		{
			this.id = id;
			this.state = state;
		}
	}
	
	public InputDevice()
	{
		buttonBuffer = new Vector<InputButton>();
		
		setBufferSize( 1 );
	}
	
	public void read()
	{
		int index;
		InputButton tempButton;
		int id;
		
		for( index = 0; index < buttonBuffer.size(); index++ )
		{
			tempButton = ( InputButton )buttonBuffer.elementAt( index );
			
			if( tempButton.state == InputButton.BUTTON_STATE_RELEASED )
			{
				
				tempButton = buttonBuffer.remove( index );
				//tempButton.state = InputButton.BUTTON_STATE_UP;
				//tempButton = new InputButton( tempButton.id, InputButton.BUTTON_STATE_UP );
				//buttonBuffer.add( tempButton );
			}
			else
			if( tempButton.state == InputButton.BUTTON_STATE_PRESSED )
			{
				tempButton = buttonBuffer.remove( index );
				tempButton.state = InputButton.BUTTON_STATE_DOWN;
				//tempButton = new InputButton( tempButton.id, InputButton.BUTTON_STATE_DOWN );
				buttonBuffer.add( tempButton );
			}
		}
	}
	
	public void setBufferSize( int size )
	{
		buttonBuffer.clear();
		buttonBufferSizeLimit = size;
	}
	
	public int getButtonState( int b )
	{
		int index;
		InputButton tempButton;
		
		for( index = 0; index < buttonBuffer.size(); index++ )
		{
			tempButton = ( InputButton )buttonBuffer.elementAt( index );
			
			if( tempButton.id == b )
			{
				return( tempButton.state );
			}
		}
		
		return( InputButton.BUTTON_STATE_UP );
	}
	
	public boolean checkButton( int b )
	{
		if( ( getButtonState( b ) == InputButton.BUTTON_STATE_PRESSED ) ||
		    ( getButtonState( b ) == InputButton.BUTTON_STATE_DOWN ) )
		{
			return( true );
		}
		else
		{
			return( false );
		}
	}
	
	public void pressButton( int b )
	{
		int index;
		InputButton tempButton;
		
		for( index = 0; index < buttonBuffer.size(); index++ )
		{
			tempButton = ( InputButton )buttonBuffer.elementAt( index );
			
			if( tempButton.id == b )
			{
				break;
			}
		}
		
		if( index == buttonBuffer.size() )
		{
			tempButton = new InputButton( b, InputButton.BUTTON_STATE_PRESSED );
			buttonBuffer.add( tempButton );
		}
	}
	
	public void releaseButton( int b )
	{
		int index;
		InputButton tempButton;
		
		for( index = 0; index < buttonBuffer.size(); index++ )
		{
			tempButton = ( InputButton )buttonBuffer.elementAt( index );
			
			if( tempButton.id == b )
			{
				buttonBuffer.remove( index );
				tempButton = new InputButton( b, InputButton.BUTTON_STATE_RELEASED );
				buttonBuffer.add( tempButton );
				break;
			}
		}
	}
}
